﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using ElectronicJournal.DB.Item;

namespace ElectronicJournal.Account.EditorAccount
{
    public partial class Publications : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           //SqlDataSource1.
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            CurrentPublicationId.Text = GridView1.SelectedDataKey.Value.ToString();
            DB.DbPublications db = new DB.DbPublications();
            DB.Item.ItemPublication item = db.GetPublicationById(GridView1.SelectedDataKey.Value.ToString());
            TitleRussianLabel.Text = item.TitleRussian;
            TitleEnglishLabel.Text = item.TitleEnglish;
            ScientificDirectionLabel.Text = db.GetScientificDirection(item.ScientificDirection);
            TypeLabel.Text = db.GetType(item.Type);
            UDCIndexLabel.Text = item.UDCIndex;
            SpecialityLabel.Text = item.Speciality;
            SummaryRussianTextBox.Text = item.SummaryRussian;
            SummaryEnglishTextBox.Text = item.SummaryEnglish;
            KeywordRussianTextBox.Text = item.KeywordsRussian;
            KeywordEnglishTextBox.Text = item.KeywordsEnglish;
            PublicationDetailsPanel.Visible = true;
            PublicationListPanel.Visible = false;
            SqlDataSource4.SelectParameters[0].DefaultValue = GridView1.SelectedDataKey.Value.ToString();
            if (item.State == 1)
            {
                ReviewPanel.Visible = true;
                ReviewFilePanel.Visible = false;
            }
            else if (item.State == 3)
            {
                ReviewPanel.Visible = false;
                ReviewFilePanel.Visible = true;
            }
            else
            {
                ReviewPanel.Visible = false;
                ReviewFilePanel.Visible = false;
            }
            if (item.PublicationReceiptNumber.Length == 0)
            {
                ReceiptNumberLabel.Text = "не оплачено";
                ReceiptScanLinkButton.Visible = false;
                PaidButton.Visible = false;
            }
            else
            {
                ReceiptNumberLabel.Text = "";
                ReceiptScanLinkButton.Visible = true;
                if (item.State == 5) PaidButton.Visible = true;
            }
        }

        protected void SetStatePaid(object sender, EventArgs e)
        {
            new DB.DbPublications().UpdatePublicationState(CurrentPublicationId.Text, 9);
            PublicationDetailsPanel.Visible = false;
            PublicationListPanel.Visible = true;
            Response.Redirect("~/Account/EditorAccount/Publications.aspx");
        }

        protected void FileReviewLinkButton_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STEAM";
            string Header = "Attachment; Filename=" + new DB.DbPublications().GetReviewFileName(CurrentPublicationId.Text);
            HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
            System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("\\Account\\files\\reviewers\\") + new DB.DbPublications().GetReviewFileName(CurrentPublicationId.Text));
            HttpContext.Current.Response.WriteFile(file.FullName);
            HttpContext.Current.Response.End();
        }

        protected void FileArticleLinkButton_Click(object sender, EventArgs e)
        {
            DB.Item.ItemPublication item = new DB.DbPublications().GetPublicationById(CurrentPublicationId.Text);
            HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STEAM";
            string Header = "Attachment; Filename=" + item.FileArticle;
            HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
            System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("\\Account\\files\\publications\\") + item.FileArticle);
            HttpContext.Current.Response.WriteFile(file.FullName);
            HttpContext.Current.Response.End();
        }

        protected void FileLetterLinkButton_Click(object sender, EventArgs e)
        {
            DB.Item.ItemPublication item = new DB.DbPublications().GetPublicationById(CurrentPublicationId.Text);
            HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STEAM";
            string Header = "Attachment; Filename=" + item.FileLetter;
            HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
            System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("\\Account\\files\\publications\\") + item.FileLetter);
            HttpContext.Current.Response.WriteFile(file.FullName);
            HttpContext.Current.Response.End();
        }

        protected void FileExpertLinkButton_Click(object sender, EventArgs e)
        {
            DB.Item.ItemPublication item = new DB.DbPublications().GetPublicationById(CurrentPublicationId.Text);
            HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STEAM";
            string Header = "Attachment; Filename=" + item.FileExpert;
            HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
            System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("\\Account\\files\\publications\\") + item.FileExpert);
            HttpContext.Current.Response.WriteFile(file.FullName);
            HttpContext.Current.Response.End();
        }

        protected void RecieptScanLinkButton_Click(object sender, EventArgs e)
        {
            DB.Item.ItemPublication item = new DB.DbPublications().GetPublicationById(CurrentPublicationId.Text);
            HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STEAM";
            string Header = "Attachment; Filename=" + item.PublicationReceiptNumber;
            HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
            System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("\\Account\\files\\publications\\") + item.PublicationReceiptNumber);
            HttpContext.Current.Response.WriteFile(file.FullName);
            HttpContext.Current.Response.End();
        }

        protected void GoToPublicationsList(object sender, EventArgs e)
        {
            PublicationDetailsPanel.Visible = false;
            PublicationListPanel.Visible = true;
            Response.Redirect("~/Account/EditorAccount/Publications.aspx");
        }

        protected void SendPublicationToReviewer(object sender, EventArgs e)
        {
            new DB.DbPublications().SendPublicationToReviewer(CurrentPublicationId.Text, ReviewersGridView.SelectedDataKey.Value.ToString());
            PublicationDetailsPanel.Visible = false;
            PublicationListPanel.Visible = true;
            Response.Redirect("~/Account/EditorAccount/Publications.aspx");
        }

        protected void SendPublicationToAuthor(object sender, EventArgs e)
        {
            new DB.DbPublications().UpdatePublicationState(CurrentPublicationId.Text, 4);
            PublicationDetailsPanel.Visible = false;
            PublicationListPanel.Visible = true;
            Response.Redirect("~/Account/EditorAccount/Publications.aspx");
        }

        protected void SetPublicationStateReady(object sender, EventArgs e)
        {
            new DB.DbPublications().UpdatePublicationState(CurrentPublicationId.Text, 5);
            PublicationDetailsPanel.Visible = false;
            PublicationListPanel.Visible = true;
            Response.Redirect("~/Account/EditorAccount/Publications.aspx");
        }
    }
}